package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.vo.FindDiagnosis;
import com.gxa.entity.SysDepartment;
import com.gxa.mapper.AttendenceMapper;
import com.gxa.mapper.SysDepartmentMapper;
import com.gxa.mapper.SysUserMapper;
import com.gxa.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysDepartmentServiceImpl implements SysDepartmentService {

    @Autowired
    SysDepartmentMapper sysDepartmentMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    AttendenceMapper attendenceMapper;


    /**
     * 分页查询所有列表
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @return R
     */
    @Override
    public R selectAllDept(Integer currentPage, Integer pageSize) {
        R r=new R();

        try {
            PageHelper.startPage(currentPage,pageSize);
            List<SysDepartment> departmentList = sysDepartmentMapper.selectAllDept();
            PageInfo<FindDiagnosis> pageInfo = new PageInfo(departmentList);
            r.setCode(200);
            r.setMsg("success");

            r.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }


    /**
     * 不分页查询所有列表
     * @return R
     */
    @Override
    public R selectAllDept() {
        R r=new R();

        try {
            List<SysDepartment> departmentList = sysDepartmentMapper.selectAllDept();
            r.setCode(200);
            r.setMsg("success");
            r.setData(departmentList);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    /**
     * 根据部门id回显部门信息
     * @param id 部门ID
     * @return R
     */
    @Override
    public R getDepartmentById(Integer id) {
        R r=new R();

        try {
            SysDepartment department = sysDepartmentMapper.getDepartmentById(id);
            r.setCode(200);
            r.setMsg("success");
            r.setData(department);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    /**
     * 根据部门id修改部门信息
     * @param sysDepartment 部门实体
     * @return R
     */
    @Override
    public R updateDeptById(SysDepartment sysDepartment) {
        R r=new R();

        try {
            sysDepartmentMapper.updateDeptById(sysDepartment);
            r.setCode(200);
            r.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }


    @Override
    public R addDepartment(SysDepartment sysDepartment) {
        R r=new R();

        try {
            sysDepartmentMapper.addDepartment(sysDepartment);
            r.setCode(200);
            r.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    /**
     * 根据部门编号修改部门排序
     * @param id 部门id
     * @return 影响行数
     */
    @Override
    public R addSortById(Integer id) {
        R r=new R();

        try {
            sysDepartmentMapper.addSortById(id);
            r.setCode(200);
            r.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }


    /**
     * 根据部门编号修改部门排序
     * @param id 部门id
     * @return 影响行数
     */
    @Override
    public R subSortById(Integer id) {
        R r=new R();

        try {
            sysDepartmentMapper.subSortById(id);
            r.setCode(200);
            r.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }


    @Override
    @Transactional(propagation= Propagation.REQUIRED,timeout=5,rollbackFor = {Exception.class})
    public R delDeptById(Integer depId) {
        R r=new R();
        if (depId!=null){
            Integer count = sysDepartmentMapper.countAllDept(depId);
            if (count==0){
                try {
                    sysDepartmentMapper.delDeptById(depId);
                    attendenceMapper.delAttendanceBydepId(depId);
                    r.setCode(200);
                    r.setMsg("success");

                }catch (Exception e){
                    throw new RuntimeException("数据库发生了异常！！！");
                }
            }else {
                r.setCode(400);
                r.setMsg("该部门还有人员不能删除");
            }

        }else {
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;

    }

    @Override
    public R findDepartmentByName(Integer currentPage, Integer pageSize, String depName) {
        R r=new R();

        try {
            PageHelper.startPage(currentPage,pageSize);
            List<SysDepartment> departmentList = sysDepartmentMapper.findDepartmentByName(depName);
            PageInfo<FindDiagnosis> pageInfo = new PageInfo(departmentList);
            r.setCode(200);
            r.setMsg("success");

            r.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }
}
